package demo1;
//无头单项非循环链表的实现
public class MyLinkedList implements IList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;


    //求链表的长度
    @Override
    public int size() {
        if(head == null) {
            return 0;
        }else {
            int len = 0;
            ListNode cur = head;
            while (cur.next != null){
                cur = cur.next;
                len++;
            }
            return len;
        }
    }

    //头插法
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head =  node;
        }else {
            node.next = head;
            head = node;

        }
    }

    //尾插法
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head =  node;
        }else{
            ListNode cur = head;
            while(cur.next!=null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //在下标 index 位置 , 插入 data
    @Override
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        int len = size();
        if(index<0||index>len){
            throw new IndexOutOfBoundsException("下标越界");
        }
        if(index == 0){
            addFirst(data);
        }
        if(index == len){
            addLast(data);
        }

        ListNode cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }

    //查找是否包含关键字key是否在单链表当中
    @Override
    public boolean contains(int key) {
        int len = size();
        if(len == 0) {
            return false;
        }
        ListNode cur = head;
        while (cur.next!=null)
        if(cur.val == key) {
            return true;
        }
        cur = cur.next;
        return false;
    }

    //删除第一次出现关键字为key的节点
    @Override
    public void remove(int key) {
        // 空链表直接返回
        if(head == null) {
            return;
        }
        // 处理头节点是目标节点的情况
        if(head.val == key) {
            head = head.next;
            return;
        }
        // 查找并删除中间/尾部的目标节点
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                // 找到目标节点，进行删除
                cur.next = cur.next.next;
                return; // 只删除第一个匹配的节点
            }
            cur = cur.next;
        }
        // 循环结束仍未找到，说明链表中没有该节点，无需操作
    }

    //删除所有值为key的节点
    @Override
    public void removeAllKey(int key) {
        if(head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null){

            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key ){
            head = head.next;
        }
    }


    //清空链表
    @Override
    public void clear() {
        ListNode cur = head;
        while (cur!=null){
            ListNode curN = cur.next;
            cur = null;
            cur = curN;
        }
        head =null;
    }

    //打印链表
    @Override
    public void display() {
        ListNode cur = head;
        while (cur!=null){
            System.out.println(cur.val+" ");
            cur = cur.next;
        }
    }
}
